package cn.louguanyang.carbon.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by louguanyang on 15/12/28.
 */
public class DateUtils {
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    private final static String DATE_PATTERN_LONG = "yyyy-MM-dd HH:mm:ss";
    /**
     * yyyy-MM-dd
     */
    private final static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

    /**
     * 当前的时间戳
     */
    public static Timestamp getNowTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    public static String getNowTimeString() {
        return format(getNowTimestamp(),DATE_PATTERN_LONG);
    }

    /**
     * 日期转换成时间戳
     *
     * @param date
     * @return
     */
    public static Timestamp toTimestamp(Date date) {
        if (date == null) return null;
        return new Timestamp(date.getTime());
    }

    /**
     * 字串转换日期
     * 默认格式 yyyy-MM-dd HH:mm:ss
     *
     * @param dateStr
     * @return
     */
    public static Date parseDate(String dateStr) {
        return parseDate(dateStr, null);
    }

    /**
     * 字串转换日期
     * 默认格式 yyyy-MM-dd HH:mm:ss
     *
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date parseDate(String dateStr, String pattern) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        if (StringUtils.isEmpty(pattern)) {
            pattern = DEFAULT_DATE_PATTERN;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字串转为时间戳
     *
     * @param dateStr
     * @return
     */
    public static Timestamp parseTimestamp(String dateStr) {
        return parseTimestamp(dateStr, null);
    }

    /**
     * 字串转为时间戳
     *
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Timestamp parseTimestamp(String dateStr, String pattern) {
        return toTimestamp(parseDate(dateStr, pattern));
    }

    /**
     * 日期转字串
     * 默认格式 yyyy-MM-dd HH:mm:ss
     *
     * @param date 时间
     * @return
     */
    public static String format(Date date) {
        return format(date, null);
    }

    /**
     * 日期转字串
     *
     * @param date    时间
     * @param pattern 格式
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (StringUtils.isEmpty(pattern)) {
            pattern = DEFAULT_DATE_PATTERN;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return new SimpleDateFormat(pattern).format(calendar.getTime());
    }

    /**
     * 在指定的日期上增加N秒并返回
     *
     * @param date
     * @param seconds
     * @return
     */
    public static Timestamp addSeconds(Date date, int seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, seconds);
        return toTimestamp(calendar.getTime());
    }

    /**
     * 在指定的日期上增加N分钟并返回
     *
     * @param date
     * @param minutes
     * @return
     */
    public static Timestamp addMinutes(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutes);
        return toTimestamp(calendar.getTime());
    }

    /**
     * 在指定的日期上增加N小时并返回
     *
     * @param date
     * @param hours
     * @return
     */
    public static Timestamp addHours(Date date, int hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hours);
        return toTimestamp(calendar.getTime());
    }

    /**
     * 在指定的日期上增加N天并返回
     *
     * @param date
     * @param days
     * @return
     */
    public static Timestamp addDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return toTimestamp(calendar.getTime());
    }

    /**
     * 在指定的日期上增加N月并返回
     *
     * @param date
     * @param months
     * @return
     */
    public static Timestamp addMonths(Date date, int months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, months);
        return toTimestamp(calendar.getTime());
    }

    /**
     * 在指定的日期上增加N年并返回
     *
     * @param date
     * @param years
     * @return
     */
    public static Timestamp addYears(Date date, int years) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, years);
        return toTimestamp(calendar.getTime());
    }

    /**
     * 获取当前日期的年份
     *
     * @return
     */
    public static int getYear() {
        return getYear(new Date());
    }

    /**
     * 获取指定日期的年份
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取当前日期的月份
     *
     * @return
     */
    public static int getMonth() {
        return getMonth(new Date());
    }

    /**
     * 获取指定日期的月份
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当然日期的日期
     *
     * @return
     */
    public static int getDay() {
        return getDay(new Date());
    }

    /**
     * 获取指定日期的日期
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

}
